package com.mimiwang.chat.config;

import com.mimiwang.common.utils.TokenUtil;
import org.apache.tomcat.websocket.server.DefaultServerEndpointConfigurator;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/5/12 20:58
 */

public class EndPointConfigurator extends DefaultServerEndpointConfigurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {

        List<String> strings = request.getHeaders().get("Sec-WebSocket-Protocol");

        if(strings.size()> 0){
            response.getHeaders().put("Sec-WebSocket-Protocol",strings);
            try {
                String id = TokenUtil.verify(strings.get(0));
                sec.getUserProperties().put("id",id);
            }catch (NullPointerException e){
                e.printStackTrace();
            }


        }else{

        }


    }
}
